<?php
/**
 * Helpers dataGrid
 * PHP version 5
 * LICENSE
 *
 * This source file is subject to the GNU/GPL that is bundled
 * with this package in the file docs/LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to fidel.oyarzo@gmail.com so we can send you a copy immediately.
 *
 * @author Fidel Oyarzo <fidel.oyarzo@gmail.com>
 */
?>
<?php if($dataGrid->table_tools){?>
<link href="<?php echo PUBLIC_PATH . 'css/datagrid/TableTools.css'?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript" src="<?php echo PUBLIC_PATH . 'default/javascript/datagrid/ZeroClipboard/ZeroClipboard.js' ?>"></script>
<script type="text/javascript" language="javascript" src="<?php echo PUBLIC_PATH . 'default/javascript/datagrid/TableTools.js' ?>"></script>
<?php } ?>


<?php if($dataGrid->datatable){?>
<link href="<?php echo PUBLIC_PATH . 'css/datagrid/demo_page.css'?>" rel="stylesheet" type="text/css" />
<link href="<?php echo PUBLIC_PATH . 'css/datagrid/demo_table.css'?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript" src="<?php echo PUBLIC_PATH . 'default/javascript/datagrid/jquery.dataTables.js' ?>"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
		$('#tableGrid').dataTable({
			"aLengthMenu": [[30, 60, 100, 200], [30, 60, 100, 200]],
			"iDisplayLength": [30],
			"sPaginationType": "full_numbers",
			<?php if($dataGrid->table_tools){?>
			"sDom": 'T<"clear">lfrtip',
			<?php } ?>
			"bJQueryUI": false,

				"oLanguage": {
				    "sProcessing": "Procesando...",
				    "sLengthMenu": "Mostrar _MENU_ registros",
				    "sZeroRecords": "No se encontraron resultados",
				    "sInfo": "Mostrando desde _START_ hasta _END_ de _TOTAL_ registros",
				    "sInfoEmpty": "Mostrando desde 0 hasta 0 de 0 registros",
				    "sInfoFiltered": "(filtrado de _MAX_ registros en total)",
				    "sInfoPostFix": "",
				    "sSearch": "Buscar:",
				    "sUrl": "",
				    "oPaginate": {
					"sFirst": "Primero",
					"sPrevious": "Anterior",
					"sNext": "Siguiente",
					"sLast": "Último"
				    }
				}			
			});
	} );
</script>
<?php } ?>
<link href="<?php echo PUBLIC_PATH . 'css/datagrid/' . $dataGrid->style . '.css'?>" rel="stylesheet" type="text/css" />
<?php if($dataGrid->create){
	echo Button::link($dataGrid->create, 'Nuevo');
}
if($dataGrid->existData()){
	//echo stylesheet_link_tag('datagrid/' . $dataGrid->style);
	
	/**
	 * Librerias para formatos de las celdas
	 */
	require  APP_PATH . 'libs/datagrid/formats.php';
	/**
	 * Utilidades
	 */
	require  APP_PATH . 'libs/datagrid/utils.php';
	
	
	//require  '../models/datagrid/datagrid.php';
		$cols			=	0;
		$button_filter		=	'';
		$button_save		=	'';
		$form			=	'';
		$end_form		=	'';
		$sources		=	array();
		$cont_options		=	0;
		$pk = $dataGrid->primary_key;
	if($dataGrid->set_check){
		$cont_options		=	1;
	}
	if($dataGrid->auto_filter){
		if(isset($_POST['sel_dtg'])){
			$sel_dtg	=	$_POST['sel_dtg'];
			$dataGrid->AutoFilter($sel_dtg);
		}else{
			$sel_dtg	=	array();
		}
		$cont_options	=	2;
		$button_filter	=	submit_tag($dataGrid->btn_caption);
		$form		=	form_tag($dataGrid->url);
		$end_form	=	end_form_tag();
	}
	if($dataGrid->save!=''){
		$button_save	=	submit_tag($dataGrid->btn_save);
		$form		=	form_tag($dataGrid->save);
		$end_form	=	end_form_tag();
	}
	$cont_options		=	$dataGrid->countOptions()+$cont_options;
	$cont_all		=	count($dataGrid->headers)+$cont_options;
	$asc_desc		=	'/asc';	
	if($dataGrid->use_paginator){
		if(count($dataGrid->data_source)>0){
			$sources			=	$dataGrid->data_source->items;
		}
	}else{
		if(count($dataGrid->data_source)>0){
			$sources	=	$dataGrid->data_source;
		}
	}
	?>
	
	<?php echo $form; ?>
	<table class='table_grilla' cellpadding="0" cellspacing="0" border="0"  id="tableGrid">
	<?php if(isset($dataGrid->title_caption)){ ?>
		<caption>
		<?php echo $dataGrid->title_caption; ?>
		</caption>
	<?php } ?>
		<thead>
		<tr>
	<?php if($dataGrid->use_paginator){
		if($dataGrid->paginator_name!='default'){
			$paginator	=	'paginators/' . $dataGrid->paginator_name;
		}else{
			$paginator	=	'datagrid/paginators/default';
		} ?>
		<th colspan="<?php echo $cont_all ?>" class='nav_tools'>
		<?php 	View::partial($paginator, false, array('page' => $dataGrid->data_source, 'show' => $dataGrid->show, 'url' => $dataGrid->getUrl())); ?>
		</th> 
	<?php } ?>
		</tr>
		<tr>
		<?php if($dataGrid->set_check==TRUE){?>
			<th>&nbsp</th>
		<?php } ?>
		<?php foreach ($dataGrid->headers as $field => $head){ ?>
				<th><?php echo $head; ?></th>
		<?php } ?>
		<?php if($cont_options>0){ ?>
			<th colspan="<?php echo $cont_options ?>">OPCIONES </th>
		<?php } ?>
		</tr>
		</thead>
		<tbody>	
		<?php if($dataGrid->auto_filter){ ?>
			<tr>
		<?php foreach ($dataGrid->getFieldsAndHeaders() as $field => $head){
				$field		=	get_field($field);
				?>
				<th><?php echo $dataGrid->printSelect($field, $sel_dtg); ?></th>
			<?php } ?>
		<?php if($cont_options>0){ ?>
				<th colspan="<?php echo $cont_options ?>"><?php echo $button_filter; ?></th>
		<?php } ?>
			</tr>
		<?php }
		foreach ($sources as $source){  ?>
			<tr>
		<?php if($dataGrid->set_check==TRUE){?>
			<td><input type='checkbox' name="check[<?php echo $source->id ?>]" class='clcheck' value='<?php echo $source->id; ?>'></td>
		<?php } ?>				
	<?php	foreach($dataGrid->fields as  $field => $key){
			$cols++;
			$value = $dataGrid->getFormat($field, $source->$field);
			$format= $dataGrid->getClass($field, $source->$field);
			$value = $dataGrid->getAlias($field, $value);
			$valueImage = $dataGrid->getImage($field, $value);
			if($valueImage!=$value){
				$value	= "<img src='";
				$value	.= PUBLIC_PATH;
				$value	.= "img/datagrid/" . $valueImage;
				$value	.= "'>";
			}   
			if(isset($dataGrid->values[$field])){
				$UModel = Util::camelcase($dataGrid->values[$field][0]);
				$campo = $dataGrid->values[$field][1];
				$model = new $UModel();
				$model->find($source->$field);
				$value = $model->$campo;
			}
	?>
			<td class='<?php echo $format ?>'><?php echo $value; ?></td>
	<?php
			if($dataGrid->getSumColumn($field)){
//				error_reporting(0);
				if(!isset($columns[$field])){
					$columns[$field] = 0;
				}
				$columns[$field] = $columns[$field] + $source->$key;
			}	
			unset($format);
		}
			if($dataGrid->edit!=''){ 	?>
				<td width="35"><a href="<?php echo PUBLIC_PATH . $dataGrid->edit .'/' . $source->$pk; ?>"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/edit.gif' ?>" class='edit'></a></td>
	<?php		}
			if($dataGrid->read!=''){ 	?>
				<td width="35"><a href="<?php echo PUBLIC_PATH . $dataGrid->read .'/' . $source->$pk; ?>"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/read.gif' ?>" class='read'></a></td>
	<?php		}
			if($dataGrid->windowRead!=''){ ?>
				<td width="35"><a href="#"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/read.gif' ?>" class='windowRead' onclick="windowRead(<?php echo $source->$pk; ?>); return false;"></a></td>
	<?php		}
			if($dataGrid->delete!=''){
				$confirm		=	'';
				if($dataGrid->delete_confirm!=''){
					$msg		=	$dataGrid->delete_confirm;
					$str		=	preg_match_all('/{[\s\w\/<>=\\\"]*}/', $msg, $items);
					$values		=	'';
					foreach ($items[0] as $item){
						$value	=	str_replace(array('{','}'), '', $item);
						$msg	=	str_replace($value, $source->$value, $msg);
						$msg	=	str_replace(array('{','}'), '', $msg);
					}
					$confirm	=	"onclick=\"return confirm('$msg');\"";
					unset($msg);
					unset($str);
					unset($values);
					unset($items);
					unset($item); ?>
	<?php 
				} ?>
				<td width="35"><a href="<?php echo PUBLIC_PATH . $dataGrid->delete .'/' . $source->$pk; ?>"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/delete.gif' ?>" class='delete' <?php echo $confirm ?>></a></td>				
	<?php
				unset($confirm);
				unset($value);
			}
			if(!empty($dataGrid->imgDown)){ 	?>
				<td width="35"><img src="<?php echo PUBLIC_PATH . 'img/datagrid/' . $dataGrid->imgDown ?>" class='manito down' title="Seleccionar"></td>
	<?php		}
			if(!empty($dataGrid->imgFacturar)){ 	?>
				<td width="35"><img src="<?php echo PUBLIC_PATH . 'img/datagrid/' . $dataGrid->imgFacturar ?>" class='manito facturar' title="Facturar"></td>
	<?php		}
			if($dataGrid->pdf!=''){ 	?>
				<td width="35"><a href="<?php echo PUBLIC_PATH . $dataGrid->pdf .'/' . $source->$pk; ?>"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/pdf.gif' ?>" class='pdf'></a></td>
	<?php		}
			if($dataGrid->excel!=''){ 	?>
				<td width="35"><a href="<?php echo PUBLIC_PATH . $dataGrid->excel .'/' . $source->$pk; ?>"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/xls.gif' ?>" class='xls'></a></td>
	<?php		}	
			if($dataGrid->codigo!=''){ 	?>
				<td width="35"><a href="<?php echo PUBLIC_PATH . $dataGrid->codigo .'/' . $source->$pk; ?>"> <img src="<?php echo PUBLIC_PATH . 'img/datagrid/codigo.gif' ?>" class='codigo'></a></td>
	<?php		}
			if($dataGrid->impresora!=''){ 	?>
				<td width="35"><img onclick="imprimir('<?php echo $source->$pk; ?>');" src="<?php echo PUBLIC_PATH . 'img/datagrid/printer.png' ?>" class='manito impresora'></td>
	<?php		}	
		$url = 'http://' . $_SERVER['SERVER_NAME'] . PUBLIC_PATH  . Router::get('controller') . '/';
		foreach($dataGrid->array_mimes as $key => $value){ ?>
		<?php if(substr($value,0,1)=='_'){ ?>
			<td><a href="<?php echo $url . substr($value, 1, strlen($value)-1) . '/' . $source->$pk; ?>" target="_blank"><img src="<?php echo PUBLIC_PATH . 'img/datagrid/' . $key . '.gif' ?>"></a></td>
		<?php }else{ ?>
			<td><a href="<?php echo  $url . $value . '/' . $source->$pk; ?>"><img src="<?php echo PUBLIC_PATH . 'img/datagrid/' . $key . '.gif' ?>"></a></td>
		<?php }
		}
		unset($key);
		unset($value); ?>
	</tr>
	<?php	} ?>
	</tbody>
	<tfoot>
	<?php	if(count($dataGrid->sum_column)>0){
			?>
			<tr>
		<?php if($dataGrid->set_check==TRUE){?>
			<td>&nbsp;</td>
		<?php } ?>				
		<?php
			foreach($dataGrid->fields as  $k => $v){
			if($dataGrid->getSumColumn($k)){
				$value = $columns[$k];
			}else{
				$value = '';
			}
		?>
		
			<td class='lblGridTotal'><?php if(is_int($value)) echo '$&nbsp;' . number_format($value,0,'.',','); ?></td>
		<?php } ?>
			</tr>
		<?php } 
	
	if($button_save!=''){
		$cols=$cols+$cont_options;
		echo	"<tr>
				<th aling='center' colspan='$cols'>$button_save</th>
			</tr>";
	}
	?>
	</tfoot>
	</table>
	<?php echo $end_form; ?>
<?php } ?>